//
//  QDMPNoticeCycleTableViewCell.m
//  QDMetroClient
//
//  Created by YueWen on 2019/2/11.
//  Copyright © 2019 YueWen. All rights reserved.
//

#import "QDMPNoticeCycleTableViewCell.h"

/// QDMainPageAdCycleTableViewCell 搭载的 UICollectionViewCell
@interface QDMainPageNoticeCycleCollectionCell: UICollectionViewCell<RITLSDCycleScrollViewCell>

/// 箭头的承载视图
@property (nonatomic, strong) UIImageView *arrowImageView;
/// 描述的
@property (nonatomic, strong) UILabel *titleLabel;

@end


@implementation QDMainPageNoticeCycleCollectionCell


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.arrowImageView = ({
            
            UIImageView *imageView = [UIImageView new];
            imageView.backgroundColor = UIColor.whiteColor;
            imageView.image = @"mp_nc_arrow_right".ritl_image;
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            
            imageView;
        });
        
        self.titleLabel = ({
            
            UILabel *label = [UILabel new];
            label.backgroundColor = [UIColor whiteColor];
            label.text = @"关于青岛地铁乘车码乘车使用公告";
            label.font = RITLLightFont(14);
            label.textColor = QDSColorDefaultBlack;
            
            label;
        });
        
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.arrowImageView];
        
        CGFloat limitMaxWidth = UIScreen.mainScreen.ritl_width - 97/*左侧的边距*/ - 44/*右侧限制的最大边距*/;
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(0);
            make.width.mas_lessThanOrEqualTo(limitMaxWidth);
            make.centerY.offset(0);
        }];
        
        [self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleLabel.mas_right).offset(4);
            make.width.height.mas_equalTo(16);
            make.centerY.offset(0);
        }];
    }
    return self;
}


- (UIImageView *)imageView {
    return nil;
}

@end



#pragma mark - TableViewCell
@implementation QDMPNoticeCycleTableViewCell

- (void)buildView {
    [super buildView];
    
    self.titleLabel = ({
        
        UILabel *label = [UILabel new];
        label.text = @"地铁公告:";
        label.font = RITLSemiboldFont(14);
        label.textColor = QDSColorDefaultGray2;
        
        label;
    });
    
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(28);
        make.top.offset(7);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(61);
    }];
    
    //追加矩形layer
    CALayer *rectLayer = CALayer.layer;
    rectLayer.backgroundColor = QDSColorTheme.CGColor;
    rectLayer.opacity = 0.3;
    rectLayer.anchorPoint = @[@0].ritl_point;
    rectLayer.bounds = @[@7,@12,@54,@7].ritl_rect;
    rectLayer.position = @[@7,@12].ritl_point;
    [self.titleLabel.layer addSublayer:rectLayer];
    
    [self.cycleView disableScrollGesture];
    self.cycleView.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.cycleView.infiniteLoop = true;
    self.cycleView.autoScroll = true;
    self.cycleView.onlyDisplayText = true;//只显示文字
    self.cycleView.titlesGroup = @[@"关于青岛地铁乘车码乘车使用公告",@"关于青岛地铁乘车码乘车使用公告关于青岛地铁乘车码乘车使用公告",@"关于青岛"];
}



- (UIEdgeInsets)cycleContentInsets {
    return UIEdgeInsetsMake(7, 97, 16, 24);
}


#pragma mark - <RITLSDCycleScrollViewDataSource>


- (Class)cycleViewCustomCollectionViewCellClass:(RITLSDCycleScrollView *)cycleView {
    return QDMainPageNoticeCycleCollectionCell.class;
}


- (UICollectionViewLayout *)cycleScollViewcustomCollectionViewLayout:(RITLSDCycleScrollView *)cycleView {
    
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    CGFloat itemHeight = UIScreen.mainScreen.ritl_width - 97 - 24;
    flowLayout.itemSize = @[@(itemHeight),@20].ritl_size;
    flowLayout.minimumLineSpacing = CGFLOAT_MIN;
    flowLayout.sectionInset = UIEdgeInsetsZero;
    
    return flowLayout;
}


- (void)cycleView:(RITLSDCycleScrollView *)cycleView setupCustomCell:(QDMainPageNoticeCycleCollectionCell *)cell forIndex:(NSIndexPath *)indexPath {
    
    cell.titleLabel.text = cycleView.titlesGroup[indexPath.item];
}



/// 用于屏蔽掉SD对flowlayout的硬修改
- (NSInteger)cycleView:(RITLSDCycleScrollView *)cycleView currentIndexWithContentOffset:(CGPoint)contentOffset {
    
    if (cycleView.contentView.ritl_width == 0 || cycleView.contentView.ritl_height == 0) {
        return 0;
    }
    
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)cycleView.contentView.collectionViewLayout;
    
    int index = 0;
    if (flowLayout.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
        index = (contentOffset.x + flowLayout.itemSize.width * 0.5) / flowLayout.itemSize.width;
    } else {
        index = (contentOffset.y + flowLayout.itemSize.height * 0.5) / flowLayout.itemSize.height;
    }
    
    return MAX(0, index);
    
}


- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    [self actionDidSelectItemAtIndex:index Info:@{}];
}

@end
